package com.yimixian.app.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;

public class PercentView extends View {

	public PercentView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView();
	}

	public PercentView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
	}

	public PercentView(Context context) {
		super(context);
		initView();
	}

	private void initView(){
		txtPaint = new Paint();
		txtPaint.setAntiAlias(true);
		density = getContext().getResources().getDisplayMetrics().density;
	}

	private Paint txtPaint;
	private float percent = 0f;
	private String text = "配送时间 23:00";
	private float txtWidth, txtHeight, txtX, txtY;
	private float density;
	private float fontSize = 16;
	private float hasArriveFontDistance;
	private Bitmap wholeBm, bmTxt, bmBar;
	private Canvas wholeCanvas, txtCanvas, barCanvas;
	private int normalColor = Color.WHITE;
	private int overrideColor = Color.BLUE;

	@Override
	protected void onDraw(Canvas canvas) {

		if(getWidth() == 0 || getHeight() == 0){
			return;
		}

		if(wholeBm != null){
			wholeBm.recycle();
		}
		if(bmTxt != null){
			bmTxt.recycle();
		}
		if(bmBar != null){
			bmBar.recycle();
		}

		txtPaint.setTextSize(fontSize * density);
		txtWidth = getFontLength(txtPaint, text);
		txtHeight = getFontHeight(txtPaint);
		txtX = (getWidth() - txtWidth) / 2;
		txtY = (getHeight() - txtHeight) / 2;

		wholeBm = Bitmap.createBitmap((int) txtWidth, (int) txtHeight, Config.ARGB_8888);
		wholeCanvas = new Canvas(wholeBm);

		bmTxt = Bitmap.createBitmap((int) txtWidth, (int) txtHeight, Config.ARGB_8888);
		txtCanvas = new Canvas(bmTxt);
		txtPaint.setColor(normalColor);
		txtCanvas.drawText(text, 0, getFontLeading(txtPaint), txtPaint);
		wholeCanvas.drawBitmap(bmTxt, 0, 0, txtPaint);

		Paint paint = new Paint();
		hasArriveFontDistance = percent * getWidth() - txtX;
		if(hasArriveFontDistance > 1){
			bmBar = Bitmap.createBitmap((int) hasArriveFontDistance, (int) txtHeight, Config.ARGB_8888);
			barCanvas = new Canvas(bmBar);
			paint.setAntiAlias(true);
			paint.setColor(overrideColor);
			barCanvas.drawRect(0, 0, hasArriveFontDistance, txtHeight, paint);
			paint = new Paint();
			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			wholeCanvas.drawBitmap(bmBar, 0, 0, paint);
		}

		paint = new Paint();
		canvas.drawBitmap(wholeBm, txtX, txtY, paint);

	}

	public void setNormalColor(int normalColor) {
		this.normalColor = normalColor;
	}

	public void setOverrideColor(int overrideColor) {
		this.overrideColor = overrideColor;
	}

	public void setFontSize(float fontSize) {
		this.fontSize = fontSize;
	}

	public void setProgressText(String text) {
		this.text = text;
	}

	public void setPercent(float percent) {
		this.percent = percent;
		postInvalidate();
	}

	public interface OnPercentListener{
		public void onPercent(float percent);
	}

	private OnPercentListener listener;

	public void setListener(OnPercentListener listener) {
		this.listener = listener;
	}

	/**
	 * @return 返回指定笔和指定字符串的长度
	 */
	public static float getFontLength(Paint paint, String str) {
		return paint.measureText(str);
	}
	/**
	 * @return 返回指定笔的文字高度
	 */
	public static float getFontHeight(Paint paint)  {
		FontMetrics fm = paint.getFontMetrics();
		return fm.descent - fm.ascent;
	}
	/**
	 * @return 返回指定笔离文字顶部的基准距离
	 */
	public static float getFontLeading(Paint paint)  {
		FontMetrics fm = paint.getFontMetrics();
		return fm.leading - fm.ascent;
	}

}
